package com.ysy.interceptor;

import com.ysy.context.SecurityContextHolder;
import com.ysy.persimmons.common.constant.SecurityConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

public class FeignRequestInterceptor implements RequestInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(FeignRequestInterceptor.class);

    @Override
    public void apply(RequestTemplate requestTemplate) {

        Long userId = SecurityContextHolder.getUserId();
        logger.info("feign 设置请求头 userId: {}", userId);
        // 若不为空，则添加到请求头中
        if ( 0L != userId ) {
            requestTemplate.header(SecurityConstants.DETAILS_USER_ID, String.valueOf(userId));
        }
    }
}
